'use strict';

//获取评论接口
module.exports = app => {
    return class CommentService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }

        /**
         * 获取文章评论
         * @param params
         */
        async getComment(params) {

            let url = this.app.urls('comment');

            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    objectId: params.id,
                    objectType: 4,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 获取文章评论详情
         * @param params
         */
        async getCommentDetail(params) {

            let url = this.app.urls('commentDetail');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    id: params.id,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 获取文章详情评论的回评
         * @param params
         */
        async getReview(params) {

            let url = this.app.urls('review');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    commentId: params.id,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 发布评论
         * @param params
         */
        async setComment(params) {

            let url = this.app.urls('setcomment');

            this.app.logger.info('---------');
            this.app.logger.info(params);
            this.app.logger.info('---------');

            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data:{
                    objectId:params.id,
                    objectType:!params.objectType? 4:params.objectType,
                    content:params.content,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /*
        回评(资讯)
         */

        async oneReviewSend(params){
            let url = this.app.urls('reviewsend');

            this.app.logger.info('---------');
            this.app.logger.info(params);
            this.app.logger.info('---------');

            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data:{
                    commentId:params.commentId,
                    content:params.content,
                    sign:params.sign,
                    reviewId:params.reviewId,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }



        /*

        社区
        根据帖子ID获取社区评论
         */
        async getPostComment(params){
            let url = this.app.urls('getpostcomment');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    forumPostId:params.id,
                    
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        //社区评论
        async setPostComment(params){
            let url = this.app.urls('setpostcomment');
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data:{
                    forumPostId:params.forumPostId,
                    content:params.content,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        //社区回评
        async towReviewSend(params){
            let url = this.app.urls('towreviewsend');
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data:{
                    commentPostId:params.commentPostId,
                    content:params.content,
                    sign:params.sign,
                    topicId:params.topicId,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        //社区评论详情
        async getPostrePlayList(params){
            let url = this.app.urls('getpostcommentDetail');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    id:params.id,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }
        //根据社区评论详情获取详情回评
        async getPostReview(params){
            let url = this.app.urls('getpostreview');
            const result = await this.ctx.fetch(url,{
                method:'get',
                dataType:'json',
                data:{
                    commentPostId:params.id,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if(data.code !=200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

    }

};
